package xjtu.se.articlecommunity.user.home.impl;


import com.vdurmont.emoji.EmojiParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xjtu.se.articlecommunity.user.home.dao.UMainviewDao;
import xjtu.se.articlecommunity.user.home.dto.ArtTabDto;
import xjtu.se.articlecommunity.user.home.dto.QueueFeedDto;
import xjtu.se.articlecommunity.user.home.dto.FeedDto;
import xjtu.se.articlecommunity.user.home.dto.SearchDto;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;

@Slf4j
@Service
@Transactional
public class FeedService {
    @Resource
    private UMainviewDao UMainviewDao;
    @Resource
    private HtmlHandler htmlHandler;

    public List<ArtTabDto> returnTabList(){
        return UMainviewDao.getTabList();
    }

    public List<QueueFeedDto> returnFeedList(String tab,String u_id){
        List<QueueFeedDto> list = UMainviewDao.getFeedList(tab,u_id);
        String htmlStr = "";
        QueueFeedDto dto = null;
        for(int i=0;i<list.size();i++){
            dto = list.get(i);
            dto.setImglist(htmlHandler.HtmlImgHandle(dto.getA_html()));
            dto.setA_html(htmlHandler.HtmlStrHandle(dto.getA_html()));
            String html = EmojiParser.parseToUnicode(dto.getA_html());
            dto.setA_html(html);
            list.set(i,dto);
        }
        return list;
    }

    public List<FeedDto> returnFollowFeed(String u_id){
        List<FeedDto> list = UMainviewDao.getFollowFeed(u_id);
        String htmlStr = "";
        FeedDto dto = null;
        for(int i=0;i<list.size();i++){
            dto = list.get(i);
            dto.setImglist(htmlHandler.HtmlImgHandle(dto.getA_html()));
            dto.setA_html(htmlHandler.HtmlStrHandle(dto.getA_html()));
            String html = EmojiParser.parseToUnicode(dto.getA_html());
            dto.setA_html(html);
            list.set(i,dto);
        }
        return list;
    }

    public List<FeedDto> returnHotFeed(String daytime, String order){
        int time = Integer.parseInt(daytime);
        int ordernum = Integer.parseInt(order);
        String date = "";
        String ordertype = "";
        switch (ordernum){
            case 1:ordertype="0.4*sub_count+0.4*com_count+0.2*like_count";break;
            case 2:ordertype="like_count";break;
            case 3:ordertype="sub_count";break;
            default:break;
        }
        if(time==0){
            date = "1970-1-1";
        }else{
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH,-1*time);

            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH)+1;
            int day = cal.get(Calendar.DAY_OF_MONTH);
            String mon = month<10?("0"+month):(""+month);
            date = year + "-" + mon + "-"+day;
        }
        List<FeedDto> list = UMainviewDao.getHotFeed(date,ordertype);
        String htmlStr = "";
        FeedDto dto = null;
        for(int i=0;i<list.size();i++){
            dto = list.get(i);
            dto.setImglist(htmlHandler.HtmlImgHandle(dto.getA_html()));
            dto.setA_html(htmlHandler.HtmlStrHandle(dto.getA_html()));
            String html = EmojiParser.parseToUnicode(dto.getA_html());
            dto.setA_html(html);
            list.set(i,dto);
        }
        return list;
    }

    public List<FeedDto> returnSearchList(SearchDto searchDto){
        String tmp = searchDto.getLine2();
        if(tmp.equals("1")){
            searchDto.setLine2("0.4*sub_count+0.4*com_count+0.2*like_count");
        }else if(tmp.equals("2")){
            searchDto.setLine2("like_count");
        }else {
            searchDto.setLine2("sub_count");
        }
        List<FeedDto> list = UMainviewDao.getsearchArtList(searchDto);
        String htmlStr = "";
        FeedDto dto = null;
        for(int i=0;i<list.size();i++){
            dto = list.get(i);
            dto.setImglist(htmlHandler.HtmlImgHandle(dto.getA_html()));
            dto.setA_html(htmlHandler.HtmlStrHandle(dto.getA_html()));
            String html = EmojiParser.parseToUnicode(dto.getA_html());
            dto.setA_html(html);
            list.set(i,dto);
        }
        return list;
    }

}
